"""历史记录记录器模块

作者: Guoyou Sun (孙国佑)
网站: http://sunguoyou.lamost.org/
版本: 1.0

版权所有 © 2025-2030 Guoyou Sun (孙国佑)
"""

import os
import json
from datetime import datetime

class HistoryLogger:
    """历史记录记录器，用于记录用户操作历史"""
    
    def __init__(self):
        """初始化历史记录记录器"""
        self.history = []
        self.history_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "history.json")
        self.load_history()
    
    def load_history(self):
        """从文件加载历史记录"""
        self.history = []
    
    def save_history(self):
        """保存历史记录到文件"""
        pass
    
    def add_record(self, action_type, description, details=None):
        """添加一条历史记录
        
        参数:
            action_type (str): 操作类型，如'open_file', 'save_file', 'process_image'等
            description (str): 操作描述
            details (dict, optional): 操作详情，可以包含任何相关信息
        
        返回:
            dict: 添加的历史记录
        """
        return {
            "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
            "action_type": action_type,
            "description": description,
            "details": details or {}
        }
    
    def get_history(self, limit=None, action_type=None):
        """获取历史记录
        
        参数:
            limit (int, optional): 限制返回的记录数量
            action_type (str, optional): 按操作类型筛选
        
        返回:
            list: 历史记录列表
        """
        filtered_history = self.history
        
        if action_type:
            filtered_history = [record for record in filtered_history if record["action_type"] == action_type]
        
        if limit and limit > 0:
            return filtered_history[-limit:]
        
        return filtered_history
    
    def clear_history(self):
        """清空历史记录"""
        self.history = []
        self.save_history()